<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../css/common.css" />
<script src="../js/common/jquery.js"></script>
<script src="../js/common/drag.js"></script>
<script src="../js/common/angular.min.js"></script>
<script type="text/javascript" src="../js/common/common.js"></script>
<script type="text/javascript" src="../js/DI.js"></script>
</head>
<body ng-app="myApp">

<div class="dqwz">
    <span>当前位置：</span>
    <span>AngularJS 依赖注入</span>
</div>

<section>
    <div ng-controller="providerController">
        <h3>【AngularJS 简单应用(provider)】</h3>
        <p><input type = "number" ng-model = "number" /></p><br/>
        <p><button ng-click = "square()">平方</button></p><br/>
        <p>结果: {{result}}</p>
        <!-- 查看源码开始 -->
        <div class="htmlCode">
            <span>【html源码】</span>
            <xmp ng-non-bindable>
<p><input type = "number" ng-model = "number" /></p><br/>
<p><button ng-click = "square()">平方</button></p><br/>
<p>结果: {{result}}</p>
            </xmp>
            <span>【js源码】</span>
            <xmp>
// 使用 provider 创建 service 定义一个方法用于计算两数乘积
app.config(function($provide){
    $provide.provider('MathService',function(){
        this.$get = function(){
            var zdyObj = {};
            zdyObj.multip = function(b,b){
                return b * b;
            }
            return zdyObj;
        }
    })
})
//在 service 中注入 provider "MathService"
app.service('zdyService',function(MathService){
    this.square = function(a){
        return MathService.multip(a,a);
    }
});
// 创建 value 对象 "defaultInput" 并传递数据
app.value('defaultInput',5);
// 将 "defaultInput" 注入到控制器
app.controller('providerController',function($scope,zdyService,defaultInput){
    $scope.number = defaultInput;
    $scope.result = zdyService.square($scope.number);
    $scope.square = function(){
        $scope.result = zdyService.square($scope.number);
    }
});
            </xmp>
            <span class="close"><a href="javascript:;">关闭</a></span>
        </div>
        <div class="viewCode">
            <span><a href="javascript:;"> >>查看源码</a></span>
        </div>
        <!-- 查看源码结束 -->
    </div>
    <div ng-controller="factoryController">
        <h3>【AngularJS 简单应用(factory)】</h3>
        <p><input type = "number" ng-model = "number" /></p><br/>
        <p><button ng-click = "square()">平方</button></p><br/>
        <p>结果: {{result}}</p>
        <!-- 查看源码开始 -->
        <div class="htmlCode">
            <span>【html源码】</span>
            <xmp ng-non-bindable>
<p><input type = "number" ng-model = "number" /></p><br/>
<p><button ng-click = "square()">平方</button></p><br/>
<p>结果: {{result}}</p>
            </xmp>
            <span>【js源码】</span>
            <xmp>
//创建 factory "MathService2" 用于两数的乘积
app.factory('MathService2',function(){
    var zdyObj = {};
    zdyObj.multip = function(b,b){
        return b * b;
    }
    return zdyObj;
})
//在 service 中注入 factory "MathService2"
app.service('zdyService2',function(MathService2){
    this.square = function(a){
        return MathService2.multip(a,a);
    }
})
// 创建 value 对象 "defaultInput2" 并传递数据
app.value('defaultInput2',8);
// 将 "defaultInput2" 注入到控制器
app.controller('factoryController',function($scope,zdyService2,defaultInput2){
    $scope.number = defaultInput2;
    $scope.result = zdyService2.square($scope.number);
    $scope.square = function(){
        $scope.result = zdyService2.square($scope.number);
    }
})
            </xmp>
            <span class="close"><a href="javascript:;">关闭</a></span>
        </div>
        <div class="viewCode">
            <span><a href="javascript:;"> >>查看源码</a></span>
        </div>
        <!-- 查看源码结束 -->
    </div>
</section>

<div class="mask_layer">
</div>

</body>
</html>